﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DSkin.Forms;
using DSkin.Controls;
using TY_Hoist.WinApp.Code;
using System.Configuration;
using System.Threading;
using System.Threading.Tasks;
using TY_WinApp.Wrapper;
using System.Runtime.InteropServices;

namespace TY_Hoist.WinApp
{
    public partial class RecSuite : Form
    {
        public RecSuite()
        {
            InitializeComponent();
        }
        RemoteControl remoteControl;
        ExeToWinform exeToWinform;
        GCDemoControl demoControl;
        public event Action FenxiVideoEvent;
        public event Action OpenMainEvent;
        bool isWaitYaoKong=true;
        private void RecSuite_Load(object sender, EventArgs e)
        {
            try
            {
                //remoteControl = new RemoteControl();
                
                exeToWinform = new ExeToWinform(this.panel1, "");
                exeToWinform.Start(System.Windows.Forms.Application.StartupPath + "/RecSuite/RecSuite.exe");
                if (StaticData.TestMujuType == 1)
                {
                    this.panel2.Location = new Point(375, 565);
                }
                else {
                    this.panel2.Location = new Point(1, 565);
                }
                this.panel2.Width =(int)(StaticData.TestJuli * 14.84);
                this.groupBox3.Text = StaticData.TestJuli + " cm";
                this.panel2.Parent = this;
                this.panel2.Visible = true;
                Thread.Sleep(1000);
                demoControl = new GCDemoControl();
                label1.Text = "正在初始化...";
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

            //Task.Factory.StartNew(() =>
            //{
            //    Thread.Sleep(1000);
            //    this.Invoke(new Action(() =>
            //    {
            //        demoControl.GetCameraList();
            //        demoControl.OpenCamera();
            //        demoControl.OpenDisplay();
            //        label1.Text = "初始化完成";
            //        button1.Enabled = true;
            //    }));
            //    while (isWaitYaoKong)
            //    {
            //        var val = remoteControl.SearchSwitch();
            //        if (val)
            //        {
            //            button1.PerformClick();
            //            break;
            //        }
            //        Thread.Sleep(100);
            //    }

            //});
        }

        private void RecSuite_FormClosing(object sender, FormClosingEventArgs e)
        {
            //try
            //{
            //    isWaitYaoKong = false;
            //    remoteControl.Clear();
            //    remoteControl.Dispose();
            //    exeToWinform.Stop();
            //}
            //catch (Exception)
            //{

            //    throw;
            //}

        }

        private void button1_Click(object sender, EventArgs e)
        {
            isWaitYaoKong = false;
            button1.Enabled = false;
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(1000);
                remoteControl.Beep(true);
                this.Invoke(new Action(() =>
                {
                    label1.Text = "正在启动加热棒...";
                }));
                
                remoteControl.DianHuo(true);
                Thread.Sleep(12000);

                this.Invoke(new Action(() =>
                {
                    label1.Text = "开始录像...";
                }));
                //remoteControl.Camera(true);
                demoControl.StartRecord();
                remoteControl.MotorRight(true);
                this.Invoke(new Action(() =>
                {
                    label1.Text = "正在转动电机...";
                }));
                Thread.Sleep(1200);
                //for (int i = 0; i < 100; i++)
                //{
                //    Thread.Sleep(20);
                //    var val = remoteControl.SearchStart();

                //    this.Invoke(new Action(() =>
                //    {

                //        this.textBox1.Text = val.ToString();

                //    }));
                //    if (val == true)
                //    {
                //        remoteControl.MotorRight(false);
                //        break;
                //    }
                //}
                remoteControl.MotorRight(false);
                button3.Enabled = true;
                remoteControl.Beep(false);
                this.Invoke(new Action(() =>
                {
                    label1.Text = "正在试验...";
                }));
                Thread.Sleep(3000);
                remoteControl.DianHuo(false);


            });
            //remoteControl.MotorRight(true);
            //remoteControl.SearchStart();
            //Thread.Sleep(10);
            //remoteControl.SearchStart();
            //Thread.Sleep(10);
            //remoteControl.SearchStart();
            //Thread.Sleep(10);
            //remoteControl.SearchStart();
            //Thread.Sleep(10);
            //remoteControl.SearchStart();
            //while (true)
            //{
            //    Thread.Sleep(10);
            //    var swich = remoteControl.SearchStop();
            //    textBox1.Text = swich.ToString();
            //    if (swich == true)
            //    {
            //        remoteControl.MotorRight(false);
            //        break;
            //    }

            //}

        }

        private void button3_Click(object sender, EventArgs e)
        {

            Task.Factory.StartNew(() =>
            {
                
                this.Invoke(new Action(() =>
                {
                    button3.Enabled = false;
                    label1.Text = "正在停止录像...";
                }));
                demoControl.StartRecord();

                
                demoControl.ClosepDisplay();
                demoControl.CloseCamera();
                //remoteControl.Camera(false);
                

                this.Invoke(new Action(() =>
                {
                    button3.Enabled = false;
                    label1.Text = "正在恢复电机...";
                }));
                remoteControl.MotorLeft(true);
                Thread.Sleep(1200);
                //for (int i = 0; i < 100; i++)
                //{
                //    Thread.Sleep(20);
                //    var val = remoteControl.SearchStop();

                //    this.Invoke(new Action(() =>
                //    {

                //        this.textBox1.Text = val.ToString();

                //    }));
                //    if (val == true)
                //    {
                //        remoteControl.MotorLeft(false);
                //        break;
                //    }
                //}
                remoteControl.MotorLeft(false);
                this.Invoke(new Action(() =>
                {
                    button1.Enabled = true;
                    label1.Text = "正在打开视频...";
                }));
                demoControl.OpenRecord();
                Thread.Sleep(2000);
                Form7 form7 = new Form7();
                form7.Show();
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(2000);
                    demoControl.RecordZhuanHuan();
                    this.BeginInvoke(new Action(() =>
                    {
                        form7.Close();
                        FenxiVideoEvent?.Invoke();
                    }));

                });


            });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            remoteControl.Camera(true);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            remoteControl.Camera(false);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            demoControl.GetCameraList();
        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {

                remoteControl.MotorRight(true);
                Thread.Sleep(1200);

                remoteControl.MotorRight(false);
            });
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {

                remoteControl.MotorLeft(true);
                Thread.Sleep(1200);

                remoteControl.MotorLeft(false);
            });
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            demoControl.OpenRecord();
        }

        private void button6_Click_1(object sender, EventArgs e)
        {
            demoControl.RecordZhuanHuan();
        }

        private void button2_Click_2(object sender, EventArgs e)
        {
            OpenMainEvent?.Invoke();

        }

        private void button4_Click_2(object sender, EventArgs e)
        {
            demoControl.OpenRecord();
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void groupBox3_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(groupBox3.BackColor);
            SizeF fontSize = e.Graphics.MeasureString(groupBox3.Text, groupBox3.Font);
            e.Graphics.DrawString(groupBox3.Text, groupBox3.Font, Brushes.MediumBlue, (groupBox3.Width - fontSize.Width) / 2, 1);
            e.Graphics.DrawLine(Pens.MediumBlue, 1, 10, (groupBox3.Width - fontSize.Width) / 2, 10);
            e.Graphics.DrawLine(Pens.MediumBlue, (groupBox3.Width + fontSize.Width) / 2 - 4, 10, groupBox3.Width - 2, 10);
        }

        private void button5_Click_2(object sender, EventArgs e)
        {
            Form7 form7 = new Form7();
            form7.Show();
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(2000);
                demoControl.RecordZhuanHuan();
                this.BeginInvoke(new Action(() =>
                {
                    form7.Close();
                    FenxiVideoEvent?.Invoke();
                }));

            });

        }
    }
}
